////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// The MIT License (MIT)                                                      //
//                                                                            //
// Copyright (c) 2013 Charles Teague                                          //
//                                                                            //
// Permission is hereby granted, free of charge, to any person obtaining a    //
// copy of this software and associated documentation files (the "Software"), //
// to deal in the Software without restriction, including without limitation  //
// the rights to use, copy, modify, merge, publish, distribute, sublicense,   //
// and/or sell copies of the Software, and to permit persons to whom the      //
// Software is furnished to do so, subject to the following conditions:       //
//                                                                            //
// The above copyright notice and this permission notice shall be included in //
// all copies or substantial portions of the Software.                        //
//                                                                            //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    //
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    //
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        //
// DEALINGS IN THE SOFTWARE.                                                  //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;

using RPSDAL;
using RPSDAL.EntityClasses;
using RPSDAL.DaoClasses;
using RPSDAL.FactoryClasses;
using RPSDAL.HelperClasses;
using RPSDAL.CollectionClasses;

using SD.LLBLGen.Pro.ORMSupportClasses;

public partial class UserCallDetails : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        grdUserCalls.PageSize = Parameters.PageSize;
        LoadUserCallDetails();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack) 
        {
            
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("SummaryReport.aspx");
    }

    private void LoadUserCallDetails()
    {
        DataTable dtFields = new DataTable();
        ResultsetFields fields = new ResultsetFields(8);

        fields.DefineField(CallFieldIndex.UserId, 0, "UserId");
        fields.DefineField(CallFieldIndex.ContactId, 1, "ContactId");
        fields.DefineField(BusinessRuleFieldIndex.From, 2, "From");
        fields.DefineField(BusinessRuleFieldIndex.To, 3, "DaysTo");
        fields.DefineField(ContactFieldIndex.Name, 4, "Name");
        fields.DefineField(ContactFieldIndex.Phone, 5, "Phone");
        fields.DefineField(ContactFieldIndex.MobilePhone, 6, "MobilePhone");
        fields.DefineField(ContactFieldIndex.ContactVisitDate, 7, "ContactVisitDate");

        RelationCollection relations = new RelationCollection();
        relations.Add(CallEntity.Relations.ContactEntityUsingContactId);
        relations.Add(CallEntity.Relations.BusinessRuleEntityUsingBusinessRuleId);


        //fields[2].ExpressionToApply = new Expression(EntityFieldFactory.Create(CallFieldIndex.Deadline), ExOp.Sub, DateTime.Now);
        fields[2].ExpressionToApply = new Expression(ContactFields.ContactVisitDate, ExOp.Add, BusinessRuleFields.From);
        fields[3].ExpressionToApply = new Expression(ContactFields.ContactVisitDate, ExOp.Add, BusinessRuleFields.To);


        IPredicateExpression filter = new PredicateExpression();
        filter.Add(CallFields.UserId == Convert.ToInt32(Global.DecryptString(Request.QueryString["id"].ToString())));
        filter.AddWithAnd(CallFields.Done != true);

        TypedListDAO dao = new TypedListDAO();
        dao.GetMultiAsDataTable(fields, dtFields, 0, null, filter, relations, false, null, null, 0, 0);

        grdUserCalls.DataSource = dtFields;
        grdUserCalls.DataBind();

        DateTime dtTemp = DateTime.Now;
        for (int i = 0; i < grdUserCalls.Rows.Count; i++)
        {
            DateTimeFormatInfo format = new DateTimeFormatInfo();
            format.LongDatePattern = "MM/dd/yyyy hh:mm:ss";
            try
            {
                dtTemp = Convert.ToDateTime(grdUserCalls.Rows[i].Cells[3].Text);//, format);
            }
            catch { }
            long timeLeft = dtTemp.Ticks - DateTime.Now.Ticks;
            long hoursLeft = (timeLeft / 10000000) / 3600;
            long minutesLeft = ((timeLeft - hoursLeft * 10000000 * 3600) / 10000000) / 60;


            grdUserCalls.Rows[i].Cells[3].Text = hoursLeft.ToString() + "h " + minutesLeft.ToString() + "min";
        }
    }

    protected void grdUserCalls_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdUserCalls.PageIndex = e.NewPageIndex;
        LoadUserCallDetails();
    }


}
